package org.sfs.storemypassword.service;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.romaframework.aspect.authentication.AuthenticationAspect;
import org.romaframework.aspect.service.annotation.ServiceClass;
import org.romaframework.aspect.session.SessionInfo;
import org.romaframework.core.Roma;
import org.romaframework.core.flow.ObjectContext;
import org.romaframework.frontend.RomaFrontend;
import org.romaframework.module.users.domain.BaseAccount;
import org.romaframework.module.users.domain.BaseProfile;
import org.romaframework.module.users.view.domain.ChangePassword;
import org.romaframework.web.service.rest.RestServiceModule;

@ServiceClass(serviceName = "direct", interfaceClass = DirectService.class, aspectImplementation = RestServiceModule.class)
public class DirectServiceImpl implements DirectService {

	public void home() {
		BaseProfile currProfile = (BaseProfile) Roma.component(AuthenticationAspect.class).getCurrentProfile();

		String homePage = "HomePage";
		if (currProfile != null && currProfile.getHomePage() != null)
			homePage = currProfile.getHomePage();

		SessionInfo currentSession = Roma.session().getActiveSessionInfo();

		// RESET THE HISTORY
		RomaFrontend.flow().clearHistory(currentSession);

		// FORWARD TO THE CONFIGURED HOME PAGE
		RomaFrontend.flow().forward(ObjectContext.getInstance().getObject(homePage), null, null, currentSession);
	}

	public void logout(HttpServletRequest iRequest, HttpServletResponse iResponse) {
		try {
			iResponse.sendRedirect(iRequest.getContextPath() + "/dynamic/common/logout.jsp");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void changePassword() {
		BaseAccount currAccount = (BaseAccount) Roma.component(AuthenticationAspect.class).getCurrentAccount();
		RomaFrontend.flow().forward(new ChangePassword(currAccount, null), null, null, Roma.session().getActiveSessionInfo());
	}
}
